/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReciteTest_MultiChoice.h"

ReciteTest_MultiChoice::ReciteTest_MultiChoice(WordBrowser* display, QWidget* parent):
	ReciteTestMethodBase(display, parent)
{
	choiceBoxes.clear();
	choiceBoxes.push_back(new QCheckBox("A", this));
	choiceBoxes.push_back(new QCheckBox("B", this));
	choiceBoxes.push_back(new QCheckBox("C", this));
	choiceBoxes.push_back(new QCheckBox("D", this));
	choiceBoxes.push_back(new QCheckBox("E", this));
	choiceBoxes.push_back(new QCheckBox("F", this));

	choiceShortcuts.clear();
	choiceShortcuts.push_back(new QShortcut(QKeySequence(Qt::Key_A + Qt::SHIFT),  this));
	choiceShortcuts.push_back(new QShortcut(QKeySequence(Qt::Key_B + Qt::SHIFT),  this));
	choiceShortcuts.push_back(new QShortcut(QKeySequence(Qt::Key_C + Qt::SHIFT),  this));
	choiceShortcuts.push_back(new QShortcut(QKeySequence(Qt::Key_D + Qt::SHIFT),  this));
	choiceShortcuts.push_back(new QShortcut(QKeySequence(Qt::Key_E + Qt::SHIFT),  this));
	choiceShortcuts.push_back(new QShortcut(QKeySequence(Qt::Key_F + Qt::SHIFT),  this));

	choiceBoxGroup = new QButtonGroup(this);
	choiceBoxGroup->setExclusive(true);

	for(int i=0; i<choiceBoxes.size(); i++)
	{
		choiceBoxGroup->addButton(choiceBoxes[i], i);
		testMethodGrid->addWidget(choiceBoxes[i], int(i/2), int(i%2));
		choiceBoxes[i]->hide();
		QObject::connect(choiceShortcuts[i],SIGNAL(activated()),choiceBoxes[i],SLOT(click()));
	}

	submitChoiceButton = new QPushButton("Sub&mit", this);
	submitChoiceButton->hide();
	testMethodGrid->addWidget(submitChoiceButton,choiceBoxes.size(),0);

	passChoiceButton = new QPushButton("&Pass", this);
	passChoiceButton->hide();
	testMethodGrid->addWidget(passChoiceButton,choiceBoxes.size(),1);

	submitChoiceShortcut = new QShortcut(QKeySequence(Qt::Key_M),  this);
	passChoiceShortcut = new QShortcut(QKeySequence(Qt::Key_P),  this);
	QObject::connect(submitChoiceShortcut,SIGNAL(activated()),submitChoiceButton,SIGNAL(clicked()));
	QObject::connect(passChoiceShortcut,SIGNAL(activated()),passChoiceButton,SIGNAL(clicked()));

	QObject::connect(choiceBoxGroup,SIGNAL(buttonClicked(int)),this,SLOT(choiceSelected(int)));
	QObject::connect(submitChoiceButton,SIGNAL(clicked()),this,SLOT(choiceSubmitted()));
	QObject::connect(passChoiceButton,SIGNAL(clicked()),this,SLOT(forget()));
	QObject::connect(passChoiceButton,SIGNAL(clicked()),this,SLOT(showMultiChoiceTestAnswer()));
}

ReciteTest_MultiChoice::~ReciteTest_MultiChoice()
{
}

void ReciteTest_MultiChoice::beginTest()
{
	//OPT.enableSpeak = false;
	OPT.lockSpeak = true;
	textDisplay->getTextBrowser()->clear();
	ReciteTestMethodBase::beginTest();
	mSelectedChoice = -1;
	showMultiChoiceButtonGroup();
	QString redBibleExp;
	redBibleExp.clear();
	Word* word = WM.getCurWord();
	if(word)
		redBibleExp =word->getRedBibleExp();
	switch(WM.getReciteTestMode())
	{
	case multi_exp_choice_method://currently not in use
		textDisplay->getTextBrowser()->clear();
		textDisplay->showEnglish();
		showMulitChoiceTestExpChioce();
		break;
	case multi_English_choice_method:
		displayHtmlString.append(QString("<table border='1' cellpadding='5'><tr><th>Choice</th><th>Explanation</th></tr><tr><td width=1%>"));
		insertMulitChoiceTestEnglishChioce();
		displayHtmlString.append(QString("</td><td>"));
		displayHtmlString.append(redBibleExp + QString("<br>"));
		insertCensoredLongmanExp();
		displayHtmlString.append(QString("</td></tr></table>"));
		//cout<<"ReciteTest_MultiChoice::beginTest-->displayHtmlString="<<displayHtmlString.toUtf8().data()<<endl;
		break;
	}

	//cout<<"ReciteTest_MultiChoice::beginTest-->displayHtmlString="<<displayHtmlString.toUtf8().data()<<endl;
	if (!displayHtmlString.isEmpty())
		textDisplay->getTextBrowser()->insertHtml(displayHtmlString);

	DM.lookup(WM.getCurWord());
}

void ReciteTest_MultiChoice::endTest()
{
	ReciteTestMethodBase::endTest();
}

void ReciteTest_MultiChoice::resetDynamicButtons()
{
	ReciteTestMethodBase::resetDynamicButtons();

	submitChoiceShortcut->setEnabled(false);
	passChoiceShortcut->setEnabled(false);
	for (int i=0; i<choiceShortcuts.size(); i++)
		choiceShortcuts[i]->setEnabled(false);
}

void ReciteTest_MultiChoice::showNextButton()
{
	resetDynamicButtons();

	for (int i=0; i<choiceBoxes.size(); i++)
		choiceBoxes[i]->hide();

	submitChoiceButton->hide();
	passChoiceButton->hide();
	ReciteTestMethodBase::showNextButton();
}

void ReciteTest_MultiChoice::showMultiChoiceButtonGroup()
{
	resetDynamicButtons();

	choiceBoxGroup->setExclusive(false);
	for(int i=0; i<choiceBoxes.size(); i++)
	{
		choiceBoxes[i]->setChecked(false);
		choiceBoxes[i]->show();
		choiceShortcuts[i]->setEnabled(true);
	}
	choiceBoxGroup->setExclusive(true);

	passChoiceButton->show();
	submitChoiceButton->show();

	submitChoiceShortcut->setEnabled(true);
	passChoiceShortcut->setEnabled(true);
}


void ReciteTest_MultiChoice::showMulitChoiceTestExpChioce()
{
	//cout<<"ReciteTest_MultiChoice::showMulitChoiceTestExpChioce"<<endl;
	mChoices.clear();
	mChoiceHead.clear();
	mChoiceHead.push_back("[A]");
	mChoiceHead.push_back("[B]");
	mChoiceHead.push_back("[C]");
	mChoiceHead.push_back("[D]");
	mChoiceHead.push_back("[E]");
	mChoiceHead.push_back("[F]");
	vector<int>wordIDs;
	WM.randomSelectWords(wordIDs, mChoiceHead.size());
	if(wordIDs.size() != mChoiceHead.size())
	{
		cout<<"ReciteTest_MultiChoice::showMulitChoiceTestExpChioce-->Error! wordIDs.size() != mChoiceHead.size()"<<endl;
		QCoreApplication::quit();
	}

	for(int i=0; i<mChoiceHead.size(); i++)
	{
		Word* word = WM.getWord(wordIDs[i]);
		if(word)
		{
			mChoices.push_back(word);
			textDisplay->getTextBrowser()->insertHtml(QString("<br>" + mChoiceHead[i]));
			textDisplay->getTextBrowser()->insertHtml(word->getRedBibleExp());
		}
		else
		{
			cout<<"DisplayArea::showMulitChoiceTestExpChioce-->Invalid word. ID = "<<wordIDs[i]<<endl;
			QCoreApplication::quit();
		}
	}
}

void ReciteTest_MultiChoice::insertMulitChoiceTestEnglishChioce()
{
	//cout<<"ReciteTest_MultiChoice::insertMulitChoiceTestEnglishChioce"<<endl;
	mChoices.clear();
	mChoiceHead.clear();
	mChoiceHead.push_back("[A]");
	mChoiceHead.push_back("[B]");
	mChoiceHead.push_back("[C]");
	mChoiceHead.push_back("[D]");
	mChoiceHead.push_back("[E]");
	mChoiceHead.push_back("[F]");
	vector<int>wordIDs;
	WM.randomSelectWords(wordIDs, mChoiceHead.size());
	if(wordIDs.size() != mChoiceHead.size())
	{
		cout<<"ReciteTest_MultiChoice::insertMulitChoiceTestEnglishChioce-->Error! wordIDs.size() != mChoiceHead.size()"<<endl;
		QCoreApplication::quit();
	}

	for(int i=0; i<mChoiceHead.size(); i++)
	{
		Word* word = WM.getWord(wordIDs[i]);
		if(word)
		{
			mChoices.push_back(word);
			displayHtmlString.append(QString("<br>") + mChoiceHead[i]);
			QString English = word->getEnglish();
			English.replace("é","e");
			displayHtmlString.append(English + QString("<br>"));
		}
		else
		{
			cout<<"DisplayArea::insertMulitChoiceTestEnglishChioce-->Invalid word. ID = "<<wordIDs[i]<<endl;
			QCoreApplication::quit();
		}
	}
}

void ReciteTest_MultiChoice::showMultiChoiceTestAnswer()
{
	//OPT.enableSpeak = true;
	OPT.lockSpeak = false;
	if(WM.getCorrectChoiceIndex() >= 0 && WM.getSelectedChoice()>= 0)
	{
		if(WM.getCorrectChoiceIndex() == WM.getSelectedChoice())
		{
			textDisplay->getTextBrowser()->clear();
			textDisplay->showEnglish();
			textDisplay->showSoundMark();
			QString answer = QString("You answer %1 is correct.<br>").arg(mChoiceHead[WM.getCorrectChoiceIndex()]);
			textDisplay->getTextBrowser()->insertHtml(QString("<br>") + answer);
			textDisplay->showAllExplanations();
		}
		else
		{
			textDisplay->getTextBrowser()->clear();
			textDisplay->showEnglish();
			textDisplay->showSoundMark();
			//textDisplay->insertHtml("");
			QString answer;
			answer.append(QString("The correct answer is %1.<br>").arg(mChoiceHead[WM.getCorrectChoiceIndex()]));
			answer.append(QString("You answer %1 is WRONG.<br>").arg(mChoiceHead[WM.getSelectedChoice()]));
			answer.append("*******************************************<br>");
			Word* word = mChoices[WM.getSelectedChoice()];
			answer.append(word->getEnglish()).append("<br>");
			answer.append(word->getSoundMark()).append("<br>");
			answer.append(word->getRedBibleExp()).append("<br>");
			answer.append("*******************************************<br>");
			textDisplay->getTextBrowser()->insertHtml(QString("<br>") + answer);
			textDisplay->showAllExplanations();
		}
	}
	else
	{
		textDisplay->getTextBrowser()->clear();
		textDisplay->showEnglish();
		textDisplay->showAllExplanations();
	}
}

void ReciteTest_MultiChoice::choiceSelected(int choice)
{
	mSelectedChoice = choice;
}

void ReciteTest_MultiChoice::choiceSubmitted()
{
	cout<<"ReciteTest_MultiChoice::choiceSubmitted()"<<endl;

	if (mSelectedChoice != -1)
	{
		if(WM.checkChoice(mSelectedChoice))
			remember();
		else
			forget();

		showMultiChoiceTestAnswer();
	}
	else
	{
		QString message="Please select a choice.";
		MessageBox::showWarning(QMessageBox::Warning,QString("Warning!"),message,12,this);
		/*QFont font;
		font.setPointSize(12);
		QMessageBox* messBox=new QMessageBox(QMessageBox::Warning,QString("Warning!"),QString(message));
		messBox->setFont(font);
		messBox->addButton(QMessageBox::Ok);
		messBox->exec();

		this->raise();
		this->activateWindow();*/
	}
}
